package org.kevin.service;

import org.kevin.bean.Employee;
import org.kevin.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

/**
 * @ClassName: EmployeeService
 * @Description: TODO
 * @Author: kevin
 * @Date: 2019/8/23 16:40
 * @Version: 1.0
 */
@Service
@CacheConfig(cacheNames = "emp")
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Cacheable(/*value = "emp",*/ key = "#p0")
    public Employee getEmp(Integer id) {
        System.out.println("查询" + id + "员工");
        Employee employee = employeeMapper.getEmployeeById(id);
        return employee;
    }

    @CachePut(/*value = "emp", */key = "#result.id")
    public Employee udateEmp(Employee employee) {
        System.out.println("update:" + employee);
        employeeMapper.updateEmp(employee);
        return employee;
    }

    @CacheEvict(/*value = "emp",*/ key = "#id", allEntries = true)
    public void deleteEmp(Integer id) {
        System.out.println("deleteEmp:" + id);
//        employeeMapper.deleteEmpById(id);

    }

    @Caching(
            cacheable = {@Cacheable(/*value = "emp",*/ key = "#lastName")},
            put = {@CachePut(/*value = "emp",*/ key = "#result.id")}
    )
    public Employee getEmployeeByLastName(String lastName) {
        System.out.println("getEmployeeByLastName:" + lastName);
        return employeeMapper.getEmpByLastName(lastName);
    }
}
